﻿@using KC.MVC.Core.Constants
@using KC.MVC.Core.Extension;
@using KC.Web.Admin.Constants
@using Microsoft.AspNetCore.Authorization
@inject IAuthorizationService AuthorizationService
@{
    ViewBag.Title = "队列连接池管理";
    Layout = "~/Views/Shared/_ListLayout.cshtml";
}

<div id="toolbar">
    <div>
        @*队列池管理-保存队列连接*@
        @if ((await AuthorizationService.AuthorizeAsync(User, "F1823EF3-2726-473E-97D4-2A4D2956E84D")).Succeeded)
        {
            <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-plus" onclick="addQueuePool()">添加</a>
        }
        @*队列池管理-保存队列连接*@
        @if ((await AuthorizationService.AuthorizeAsync(User, "F1823EF3-2726-473E-97D4-2A4D2956E84D")).Succeeded)
        {
            <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-pencil" onclick="editQueuePool()">编辑</a>
        }
        @*队列池管理-删除队列连接*@
        @if ((await AuthorizationService.AuthorizeAsync(User, "36497733-7F9B-43D4-A7A6-A31ABC11CB4D")).Succeeded)
        {
            <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-trash" onclick="removeQueuePool()">删除</a>
        }
    </div>

    <div>
        <label>访问名称:</label><input id="selectname" class="easyui-textbox" style="width:200px" data-options="prompt:'请输入查询值'" />
        <a href="javascript:void(0)" onclick="searchData()" class="easyui-linkbutton" data-options="iconCls:'fa fa-search'">查询</a>
        <a href="javascript:void(0)" onclick="RefreshSearch()" class="easyui-linkbutton" data-options="iconCls:'fa fa-refresh'">刷新</a>
    </div>
</div>
<table id="datagrid"></table>
@section scripts
{
<script type="text/javascript">
        $(function() {
            InitDataGrid();
        });

        var loadDataUrl = '@Url.Action(ActionName.QueuePool.LoadQueuePoolList, ControllerName.QueuePool)';
        function InitDataGrid() {
            var $dataGrid = $('#datagrid').datagrid({
                url: loadDataUrl,
                method: 'get',
                idField: 'QueuePoolId',
                striped: true,
                fitColumns: true,
                rownumbers: true,
                singleSelect: true,
                pagination: true,
                nowrap: false,
                fit: true,
                pageSize: 10,
                pageList: [10, 20, 50, 100],
                showFooter: false,
                toolbar: "#toolbar",
                //toolbar: [{ text: document.getElementById('toolbar') }],
                columns: [
                    [
                        { field: 'QueuePoolId', title: 'Id', width: 30, align: 'left' },
                        { field: 'CloudTypeString', title: '云服务', width: 50, align: 'left' },
                        { field: 'QueueTypeString', title: '类型', width: 80, align: 'left' },
                        { field: 'Endpoint', title: '访问地址', width: 200, align: 'left' },
                        { field: 'AccessName', title: '访问名称', width: 100, align: 'left' },
                        //{ field: 'CreatedBy', title: '创建人', width: 100, align: 'left' },
                        //{ field: 'CreatedDate', title: '创建时间', formatter: Common.DateFormatter, width: 100, align: 'left' },
                        { field: 'PasswordExpiredTime', title: '密码过期时间', formatter: Common.DateFormatter, width: 100, align: 'left', sortable: true },
                        { field: 'TenantCount', title: '租户个数', width: 50, align: 'left', editor: { type: 'numberbox', options: { min: 20, precision: 0 } } }
                    ]
                ],
                onBeforeLoad: function(param) {
                },
                onLoadSuccess: function(data) {
                },
                onLoadError: function() {
                }
            });
            $('#toolbar').keydown(function() {
                if (event.keyCode == "13") {
                    searchData();
                }
            });
        }

        function RefreshSearch() {

            $("#selectname").searchbox('setValue', '');
            searchData();
        }
        function searchData() {
            $('#datagrid').datagrid('unselectAll');
            $('#datagrid').datagrid('load', { selectname: $("#selectname").searchbox("getValue") });
        }

        function addQueuePool() {
            openForm(0);
        }
        function editQueuePool() {
            var row = $('#datagrid').datagrid('getSelected');
            if (row) {
                openForm(row.QueuePoolId);
            } else {
                $.messager.showErrorCenter("系统提示", "请选择需要编辑的数据", 2000);
            }
        }

        var getFormUrl = '@Url.Action(ActionName.QueuePool.GetQueuePoolForm, ControllerName.QueuePool)';
        var saveFormUrl = '@Url.Action(ActionName.QueuePool.SaveQueuePoolForm, ControllerName.QueuePool)';
        function openForm(id) {
            var dialog = $.easyui.showDialog({
                title: "保存数据",
                width: 400,
                height: 300,
                href: getFormUrl + "?id=" + id,
                model: true,
                topMost: false,
                enableHeaderContextMenu: false,
                enableApplyButton: false,
                onSave: function(d) {
                    var validate = d.form("enableValidation").form("validate");
                    if (validate) {
                        $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                        $.ajax({
                            async: true,
                            type: 'post',
                            dataType: 'json',
                            url: saveFormUrl,
                            data: AddAntiForgeryToken(d.form("getData")),
                            success: function(data) {
                                if (data.success) {
                                    d.window('close');
                                    if (data.Result) {
                                        $("#datagrid").datagrid('reload');
                                        $.messager.showInfoCenter('系统提示', '保存数据成功', 1000);
                                    } else {
                                        $.messager.showInfoCenter('错误信息', '保存数据失败', 1000);
                                    }
                                } else {
                                    $.messager.showInfoCenter('错误信息', data.message, 1000);
                                }
                            },
                            complete: function() {
                                $.easyui.loaded();
                            }
                        });
                        return false;
                    } else {
                        return false;
                    }
                },
                onLoad: function() {
                    var f = $(this), ret = $.fn.dialog.defaults.onLoad();
                    f.form("disableValidation").form("enableValidation");
                    return ret;
                }
            });
        }

        var removeUrl = '@Url.Action(ActionName.QueuePool.RemoveQueuePool, ControllerName.QueuePool)';
        function removeQueuePool() {
            var row = $('#datagrid').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否确定删除该条记录?', function(r) {
                    if (r) {
                        $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                        $.post(removeUrl, { id: row.QueuePoolId }, function(data) {
                            $.easyui.loaded();
                            if (data.success) {
                                if (data.Result) {
                                    $('#datagrid').datagrid('reload');
                                    $.messager.showInfoCenter('系统提示', '删除数据成功。', 1000);
                                } else {
                                    $.messager.showErrorCenter('错误消息', '删除数据失败。', 1000);
                                }
                            } else {
                                $.data.showErrorCenter('错误消息', data.message, 1000);
                            }
                        }, 'json');
                    }
                });
            } else {
                $.messager.showErrorCenter("系统提示", "请选择需要删除的数据", 2000);
            }

        }

        var getPasswordHashUrl = '@Url.Action(ActionName.Tenant.GeneratePasswordHash, ControllerName.Tenant)';
        function generatePasswordHash() {
            //debugger;
            var password = $('#AccessKeyPassword').val();
            if (password == '') {
                $.messager.showErrorCenter('错误消息', "用户密码不能为空", 1000);
                return;
            }
            $.messager.confirm('确认', '是否根据秘钥:（' + password + '）进行加密，生成加密后的秘钥?', function(r) {
                if (r) {
                    $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                    $.post(getPasswordHashUrl, { password: password }, function(result) {
                        $.easyui.loaded();
                        if (result.success) {
                            $('#AccessKeyPasswordHash').val(result.message);
                        } else {
                            $.messager.showErrorCenter('错误消息', result.message, 1000);
                        }
                    }, 'json');
                }
            });
        }

        var testConnectionUrl = '@Url.Action(ActionName.QueuePool.TestQueueConnection, ControllerName.QueuePool)';
        function testConnection() {
            //debugger;
            var cloudType = $('#CloudType').val();
            if (cloudType == '') {
                $.messager.showErrorCenter('错误消息', "云类型不能为空", 1000);
                return;
            }
            var endpoint = $('#Endpoint').val();
            if (endpoint == '') {
                $.messager.showErrorCenter('错误消息', "服务器地址不能为空", 1000);
                return;
            }
            var accessName = $('#AccessName').val();
            if (accessName == '') {
                $.messager.showErrorCenter('错误消息', "服务器AccessName不能为空", 1000);
                return;
            }
            var accessKeyHash = $('#AccessKeyPasswordHash').val();
            if (accessKeyHash == '') {
                $.messager.showErrorCenter('错误消息', "服务器AccessKey不能为空", 1000);
                return;
            }
            $.easyui.loading({ msg: '正在保存数据，请稍等...' });
            $.post(testConnectionUrl,
                {
                    CloudType: cloudType,
                    Endpoint: endpoint,
                    AccessName: accessName,
                    AccessKeyPasswordHash: accessKeyHash,
                },
                function(data) {
                    $.easyui.loaded();
                    if (data.success) {
                        if (data.Result == '') {
                            $.messager.showInfoCenter('系统提示', '测试消息队列连接成功。', 1000);
                        } else {
                            $.messager.showErrorCenter('错误消息', data.Result, 3000);
                        }
                    } else {
                        $.messager.showErrorCenter('错误消息', data.message, 1000);
                    }
                }, 'json');
        }
</script>
}

